package com.qf.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.qf.mapper.DtsUserMapper;
import com.qf.mapper.StatMapper;
import com.qf.pojo.DtsUser;
import com.qf.service.DtsUserService;
import com.qf.vo.DayCountStatis;
import com.qf.vo.DayStatis;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.List;

/**
 * @author wanghaotian
 * time:2022/12/16,0016 10:11
 * description:
 */

@Service
public class DtsUserServiceImpl implements DtsUserService {

    @Autowired
    private DtsUserMapper userMapper;

    @Autowired
    private StatMapper statMapper;

    @Override
    public int count() {
        LambdaQueryWrapper<DtsUser> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(DtsUser::getDeleted,false);
        return userMapper.selectCount(wrapper);
    }

    @Override
    public List<DayStatis> findUserDayStatis() {
        return statMapper.findUserDayStatis();
    }

    @Override
    public IPage<DtsUser> findPage(Integer page, Integer limit, String mobile, String username, String sort, String order) {
        IPage<DtsUser> iPage = new Page<>(page,limit);
        QueryWrapper<DtsUser> wrapper = new QueryWrapper<>();
        if (!StringUtils.isEmpty(mobile)) {
            wrapper.like("mobile", mobile);
        }
        if (!StringUtils.isEmpty(username)) {
            wrapper.like("username", username);
        }
        wrapper.eq("deleted", false);
        if (!StringUtils.isEmpty(sort) && !StringUtils.isEmpty(order)) {
            if (order.equalsIgnoreCase("desc")) {
                wrapper.orderByDesc(sort);
            }else {
                wrapper.orderByAsc(sort);
            }
        }
        iPage = userMapper.selectPage(iPage, wrapper);
        return iPage;
    }

    @Override
    public List<DayCountStatis> findUserCount(int i) {
        return statMapper.findUserCount(i);
    }
}
